import { defineStore } from "pinia";
import { ref } from "vue";

export const useUserStore = defineStore("user", () => {
  const token = ref(localStorage.getItem("token") || "");
  const userInfo = ref({});

  const login = (newToken, newUserInfo) => {
    token.value = newToken;
    userInfo.value = newUserInfo;
    localStorage.setItem("token", newToken);
  };

  const logout = () => {
    token.value = "";
    userInfo.value = {};
    localStorage.removeItem("token");
  };

  const updateUserInfo = (newInfo) => {
    userInfo.value = { ...userInfo.value, ...newInfo };
  };

  return {
    token,
    userInfo,
    login,
    logout,
    updateUserInfo,
  };
});
